package com.sh.data.engine.application.service.integration;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.DataSourceDTO;
import com.sh.data.engine.application.model.dto.integration.DataSourceDetailDTO;
import com.sh.data.engine.application.model.dto.integration.HiveSiteXmlUploadDTO;
import com.sh.data.engine.application.model.mapper.integration.*;
import com.sh.data.engine.application.model.request.integration.*;
import com.sh.data.engine.common.exception.BusinessException;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.base.model.FileUploadResultDomain;
import com.sh.data.engine.domain.base.service.FileService;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceConnectParam;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceQueryParam;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceSaveParam;
import com.sh.data.engine.domain.integration.datasource.service.DataSourceService;
import com.sh.data.engine.domain.workspace.manager.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;


/**
 * @author: mengzheng.mhc
 * @date: 2024/6/3 14:00
 */
@Slf4j
@Service
public class DataSourceApplicationService {

    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private FileService fileService;

    @Autowired
    private DataSourceDTOMapper dataSourceDTOMapper;

    @Autowired
    private DataSourcePageMapper dataSourcePageMapper;

    @Autowired
    private DataSourceSaveParamMapper dataSourceSaveParamMapper;

    @Autowired
    private DataSourceConnectParamMapper dataSourceConnectParamMapper;

    @Autowired
    private FileUploadResultMapper fileUploadResultMapper;

    @Value("${data.engine.hiveUploadAddress:hive-site/}")
    private String hiveUploadAddress;

    public PagerResponse<DataSourceDTO> getDataSourcePageList(
        String dsType, String dsName, Integer status, Integer pageNum, Integer pageSize) {
        Long projectId = ServletUtils.getProjectId();
        var param = DataSourceQueryParam.builder()
            .dsType(dsType)
            .dsName(dsName)
            .status(status)
            .projectId(projectId)
            .pageNum(pageNum)
            .pageSize(pageSize)
            .build();
        var dsPageList = dataSourceService.getDataSourcePageList(param);
        return dataSourcePageMapper.toPageResponse(dsPageList);
    }

    public boolean saveDataSource(DataSourceSaveRequest dataSourceSaveRequest) {
        DataSourceSaveParam dataSourceParam = dataSourceSaveParamMapper.map(dataSourceSaveRequest);
        dataSourceService.saveDataSource(dataSourceParam);
        return true;
    }

    public boolean dropDataSourceById(Long id) {
        return dataSourceService.dropDataSourceById(id, ServletUtils.getProjectId());
    }

    public DataSourceDetailDTO getDataSourceDetailById(Long id) {
        var dataSourceDetailDomain = dataSourceService.getDataSourceDetailById(id, true);
        return dataSourceDTOMapper.domainToDTO(dataSourceDetailDomain);
    }

    public String getLastUpdateTime() {
        return dataSourceService.getLastUpdateTime();
    }

    public List<String> getSchemas(DataSourceSchemaQueryRequest request) {
        return storageService.getSchemas(
            request.getDsLink(), request.getUsername(), request.getPassword(), 0);
    }

    public boolean tableExist(Long dsId, String tableName) {
        return dataSourceService.tableExist(dsId, tableName);
    }

    public boolean testDataSourceConnection(DataSourceConnectionRequest request) {
        DataSourceConnectParam connectParam = dataSourceConnectParamMapper.map(request);
        return dataSourceService.testDataSourceConnection(connectParam);
    }

    public HiveSiteXmlUploadDTO uploadHiveSiteXml(MultipartFile multipartFile) {
        String hiveSiteAddress = this.hiveUploadAddress + "/" + UUID.randomUUID();
        try {
            FileUploadResultDomain upload = fileService.uploadHiveSite(multipartFile, hiveSiteAddress, false);
            log.info("上传Hive site xml成功");
            return fileUploadResultMapper.domainToDTO(upload);
        } catch (Exception e) {
            throw new BusinessException("上传Hive site xml失败");
        }
    }
    @Autowired
    private DSCreateUpdateImpl dsCreateUpdate;

    public Boolean createDataSource(DataSourceCreateRequest dataSourceCreateRequest) {
        DataSourceSaveParam map = dsCreateUpdate.map(dataSourceCreateRequest);
        dataSourceService.saveDataSource(map);
        return true;
    }

    public Boolean updateDataSource(DataSourceCreateRequest request) {
        DataSourceSaveParam map = dsCreateUpdate.map(request);
        dataSourceService.saveDataSource(map);
        return true;
    }

    @Autowired
    private DataSourceConnParamMapper dataSourceConnParamMapper;

    public boolean testDataSource(DataSourceGeneralConnectionRequest request) {
        DataSourceConnectParam connectParam = dataSourceConnParamMapper.map(request);
        return dataSourceService.testDataSourceConnection(connectParam);
    }
}
